package com.hx.base.zzw;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 * 描述:
 * 服务器端
 *
 * @author WeiGu
 * @create 2019-08-15 19:06
 */
public class TCPServer {
    public static void main(String[] args) throws IOException {
        //创建服务器端套接字对象
        ServerSocket server = new ServerSocket(12345);
        //获取连接
        Socket socket = server.accept();
        InputStream is = socket.getInputStream();

        File file = new File("D:\\upload");
        if (!file.exists()) {
            file.mkdir();
        }
        String fileName = "D:\\upload" + "\\briup" + System.currentTimeMillis() + new Random().nextInt(9999) + ".jpg";

        FileOutputStream fos = new FileOutputStream(fileName);
        byte[] bytes = new byte[2048];
        int length;
        while ((length = is.read(bytes)) != -1) {
            fos.write(bytes, 0, length);
            fos.flush();
        }
        //这句代码不能写在这里
        //socket.shutdownOutput();

        System.out.println("来了没");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("上传成功".getBytes());

        fos.close();
        socket.close();
        server.close();

    }
}
